﻿using HtmlAgilityPack;
using Spider.Analyse;
using Spider.Entitys.天气预报各级实体对象;
using Spider.Rules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Spider.HTMLAnalyse.天气预报各级HTML解析
{
    public class 天气预报每天详情解析 : AnalyseBase, IAnalyse
    {
        public override List<T> GetListData<T>(HtmlDocument doc, IRule rule, object o = null)
        {
            List<T> listEntity = new List<T>();
            //更加xpath获取总的对象，如果不为空，就继续选择dl标签
            HtmlNode res = doc.DocumentNode.SelectSingleNode(rule.RuleXPathString());
            if (res != null)
            {
                var h1 = res.SelectNodes("h1");
                var table = res.SelectNodes("table");
                var bb = res.SelectNodes("b");
                var font = res.SelectNodes("font");


                气温 day = new 气温()
                {
                    天气状况 = table[0].SelectNodes("tr")[1].SelectNodes("td")[1].InnerText.Trim(),
                    温度气温 = table[0].SelectNodes("tr")[2].SelectNodes("td")[1].InnerText.Trim(),
                    风力风向 = table[0].SelectNodes("tr")[3].SelectNodes("td")[1].InnerText.Trim()
                };
                气温 night = new 气温()
                {
                    天气状况 = table[0].SelectNodes("tr")[1].SelectNodes("td")[2].InnerText.Trim(),
                    温度气温 = table[0].SelectNodes("tr")[2].SelectNodes("td")[2].InnerText.Trim(),
                    风力风向 = table[0].SelectNodes("tr")[3].SelectNodes("td")[2].InnerText.Trim()
                };
                每天详细的天气情况 entity = new 每天详细的天气情况()
                {
                    农历 = res.InnerText.AnalyseRegexResult("农历:(.*?)&nbsp;"),
                    阳历 = res.InnerText.AnalyseRegexResult("阳历:(.*?)&nbsp;"),
                    日期 = h1[0].InnerText.AnalyseRegexResult("(.*年.*月.*日)").Trim(),
                    祭祀宜 = res.InnerText.AnalyseRegexResult("宜: (.*?)忌"),
                    祭祀忌 = res.InnerText.AnalyseRegexResult("忌: (.*?)\r\n"),
                    星座 = res.InnerText.AnalyseRegexResult("星座: (.*?)\r\n"),
                    气温集合 = new List<气温>()
                        {
                            day,
                            night
                        }
                };
                listEntity.Add((T)(object)entity);
            }
            return listEntity;
        }
    }
}
